package PokerComponents;

import DataStructures.PokerPlayer;
import Network.RemotePokerPlayer;

/**
 * This class is used for cycling through players in a poker hand. A dealer can get the next player and interact with them. 
 * If the player folds the dealer can remove them from the queue. This classes intention is to make iterating over players
 * in the hand easier. 
 * @author Phil
 *
 */
public class PlayerIterator {
	
	private RemotePokerPlayer[] players;
	private int activePlayerIndex;
	
	public PlayerIterator(RemotePokerPlayer[] players){
		this.players = players;
		this.activePlayerIndex = 0;
	}
	
	public PlayerIterator(RemotePokerPlayer[] players, int seatNumber){
		this.players = players;
		this.activePlayerIndex = seatNumber;
	}
	
	/**
	 * Returns the active player, null if no players at the table
	 * @return
	 */
	public RemotePokerPlayer getActivePlayer(){
		return this.players[activePlayerIndex];
	}
	
	
	/**
	 * Set the next player in the hand as the active player
	 * @return
	 */
	public void gotoNextPlayer(){
		PokerPlayer p = null;
		if(size() > 1){
			while(p == null){				
				this.activePlayerIndex++;				
				this.activePlayerIndex %= players.length;
				p = players[activePlayerIndex];
			}	
		}		
	}
	
	/**
	 * Remove the current active player from the hand and return the next player. If only one player
	 * remains after removing one then return null
	 * TODO: this method probably removes the wrong player, too JUnit!
	 * @return
	 */
	public RemotePokerPlayer removeAndGotoPlayer(){
		players[activePlayerIndex] = null;
		gotoNextPlayer();
		return getActivePlayer();
	}
	
	/**
	 * Return the number of players left in the hand
	 * @return
	 */
	public int size(){
		int n = 0;
		for(int i=0; i<players.length; i++){
			if(players[i] != null){
				n++;
			}
		}
		return n;
	}
	
	public void setActivePlayer(int seatNumber){
		this.activePlayerIndex = seatNumber;
	}
	
	public boolean isPlayerInHand(int seatNumber){
		boolean playerFound = false;
		if(this.players[seatNumber] != null){
			playerFound = true;
		}
		return playerFound;
	}
	
}
